package com.xbxkj.mix.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.nosql.redis.RedisDS;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.system.SystemUtil;
import com.alibaba.fastjson.JSONObject;
import com.xbxkj.mix.bean.User;
import com.xbxkj.mix.config.WebScoketService;
import com.xbxkj.mix.util.AuthSign;
import com.xbxkj.mix.util.Contant;
import com.xbxkj.mix.util.result.ApiResult;
import com.xbxkj.mix.util.steam.CacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-08-06
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult<List<User>> test() {

        User user = new User();
        userService.save(user);
        log.debug("保存成功");
        return ok(userService.list());
    }

    @RequestMapping(value = "/yzm", method = RequestMethod.GET)
    @ResponseBody
    public void yzm() {

        String sessionId = getSession().getId();
        //定义图形验证码的长和宽
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100,6,100);
        jedis().set(sessionId, captcha.getCode());
        jedis().expire(sessionId,60);
        try {
            captcha.write(getResponse().getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult login(String code) {

        String sessionId=getSession().getId();
        if (StrUtil.hasBlank(code)) {
            return fail("请输入图形验证码");
        }
        if(!jedis().exists(sessionId)){
            return  fail("验证码已失效");
        }
        //获取缓存的验证码
        String cacheCode = jedis().get(sessionId) ;
        //比较缓存和输入是否相等 忽略大小写
        if (StrUtil.equals(code, cacheCode,true)) {
            String token = null;
            try {
                User user = userService.list().get(0);
                token = AuthSign.sign(user.getId(), JSONObject.toJSONString(user));
            } catch (UnsupportedEncodingException e) {
                return fail("令牌创建失败");
            }
            //登陆成功 删除缓存验证码
            jedis().del(sessionId);
            return ok(token);
        }else{
            return fail("验证码输入错误");
        }
    }



}
